package com.syni.mdd.sjb.common.datatreaure.handler.news;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppNews;
import com.syni.mdd.sjb.common.component.utils.common.spring.ApplicationContextHolder;
import com.syni.mdd.sjb.common.component.utils.common.utils.RequestHeaderData;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.dataBankBusiness.NewDetailParamsRpVo;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserChannelService;

import java.util.Map;

public class OrderDynamicsHandler extends NewsHandler {
    public void handle(DxshAppNews dxshAppNews, NewDetailParamsRpVo newDetailParamsRpVo){
        newDetailParamsRpVo.setStoreId(dxshAppNews.getStoreId());
        if(dxshAppNews.getMessageSecondType() == Constants.DXSH_APP_NEW_ORDER_MDD_STATUS){
            newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.MESSAGE_TYPE_MDD_GROUP_COUPON);
            Map<String,Object> dataMap = JSON.parseObject(dxshAppNews.getContent());
            if(dataMap.containsKey("bmsOrderId")){
                newDetailParamsRpVo.setMddOrderId(Integer.parseInt(dataMap.get("bmsOrderId").toString()));
            }
            if(dataMap.containsKey("bmsGroupBuyUseId")){
                newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.MESSAGE_TYPE_MDD_GROUP_BUY);
                newDetailParamsRpVo.setMddGroupBuyUseId(Integer.parseInt(dataMap.get("bmsGroupBuyUseId").toString()));
            }
        }else if(dxshAppNews.getMessageSecondType() == Constants.DXSH_APP_NEW_ORDER_MINAPP_STATUS){
            newDetailParamsRpVo.setStoreId(dxshAppNews.getStoreId());
            RequestHeaderData requestHeaderData = new RequestHeaderData();
            DxshAppUserChannelService dxshAppUserChannelService = ApplicationContextHolder.getBean(DxshAppUserChannelService.class);
            String token = requestHeaderData.getString("token");
            if(Integer.parseInt(dxshAppNews.getCode()) == NewDetailParamsRpVo.APPLET_FAILED_PRINT_CODE){
                newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.APPLET_FAILED_PRINT_CODE);
            }else {
                String minAppIndex = dxshAppUserChannelService.getMinAppIndexUrl();
                if(StringUtils.isNotBlank(minAppIndex)) {
                    newDetailParamsRpVo.setJumpUrl(minAppIndex);
                    newDetailParamsRpVo.setJumpUrl(String.format("%s?userToken=%s",minAppIndex,token));
                }
                Map<String,Object> dataMap = JSON.parseObject(dxshAppNews.getContent());
                newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.MESSAGE_TYPE_MINAPP_ORDER);
                if(dataMap.containsKey("newAction")){
                    Integer newAction = Integer.parseInt(dataMap.get("newAction").toString());
                    if(newAction == NewDetailParamsRpVo.JUMP_ACTION){
                        String jumpToUrl = dataMap.get("jumpTo").toString();
                        newDetailParamsRpVo.setJumpUrl(String.format("%s&userToken=%s",jumpToUrl,token));
                        newDetailParamsRpVo.setAction(NewDetailParamsRpVo.JUMP_ACTION);
                    } else {
                        newDetailParamsRpVo.setAction(NewDetailParamsRpVo.JUMP_INDEX);
                    }
                }
            }
        }
    }
}
